package com.chineseall.app.dao.mongo;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * loc {x:纬度,y:纬度}
 * @author wbxie
 * 2013-11-21
 */
@Repository
public class Location extends AppMongo {

    /**
     * 查找附件的人
     * @param lat
     * @param lon
     * @param limit 取多少条
     * @param uid>0 查找uid 存在的
     * @return
     */
    public List<DBObject> near(double lat, double lon, int limit, int uid) {
        DBObject loc = builder("$near", new BasicDBObject("x", lat).append("y", lon)).get();
        DBObject query = builder("loc", loc).get();
        if (uid > 0)
            query.put("uid", builder("$gt", ONE).get());
        return getColl().find(query).limit(limit).toArray();
    }

    /**
     * 
     * @param lat
     * @param lon
     * @param distance 最远距离
     * @return
     */
    public List<DBObject> nearDistance(double lat, double lon, double distance) {
        DBObject loc = builder("$near", new BasicDBObject("x", lat).append("y", lon)).append("$maxDistance", distance)
                .get();
        return getColl().find(builder("loc", loc).get()).toArray();
    }

    public List<DBObject> onlineList(int limit, int uid) {
        DBObject query = null;
        if (uid > 0)
            query = builder("uid", builder("$gt", ONE).get()).get();
        return getColl().find(query).sort(builder("t", -ONE).get()).limit(limit).toArray();
    }

}
